package com.vitabuybuy.tv.dialog

import android.content.Context
import android.view.View
import android.view.animation.Animation
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.*
import com.vitabuybuy.tv.dto.CouponCenterDto
import com.vitabuybuy.tv.inter.OnCouponCenterClickListener
import com.library.common.base.BindRecycAdapter
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

/**
 * 优惠券弹窗
 *
 */
class StoreCouponsPopup(
    context: Context,
    couponsList: MutableList<CouponCenterDto>,
    private val fromType: Int,
    private val onConfirm: (coupon: CouponCenterDto, position: Int) -> Unit
) : BasePopupWindow(context) {
    companion object {
        const val USER_COUPONS = 0X11
        const val RECEIVER_COUPONS = 0X12
    }
    init {
        couponsAdapter.fromType = fromType
        couponsAdapter.setNewData(couponsList)
    }

    private lateinit var mBinding: PopupCouponsBinding

    lateinit var couponsAdapter: CouponsAdater


    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_coupons)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as PopupCouponsBinding

        couponsAdapter = CouponsAdater()

        mBinding.listview.apply {
            adapter = couponsAdapter
            addItemDecoration(RecycleViewDivider(context, DensityUtil.dip2px(1f)))
            layoutManager = LinearLayoutManager(context)
        }
        couponsAdapter.itemSaveCoupons = { item, position ->
            onConfirm.invoke(item, position)
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }

    class CouponsAdater : BindRecycAdapter<ItemCoupons1Binding, CouponCenterDto>() {
        var fromType: Int? = null
        override fun onBindViewHolder(
            mBinding: ItemCoupons1Binding,
            itemData: CouponCenterDto,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            when (fromType) {
                RECEIVER_COUPONS -> mBinding.btnGet.text = mContext.getString(R.string.get_1)
                USER_COUPONS -> mBinding.btnGet.text = mContext.getString(R.string.use_now)
            }
            mBinding.itemBean = itemData
            mBinding.position = position
            mBinding.listenner = object : OnCouponCenterClickListener {
                override fun onItemClick(item: CouponCenterDto, position: Int) {
                    if (item.isReceived) return
                    itemSaveCoupons.invoke(item, position)
                }
            }
        }

        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_coupons_1
        }

        lateinit var itemSaveCoupons: (item: CouponCenterDto, position: Int) -> Unit
    }
}